library PkAbility initializer Init requires entities, utils

    globals
        public integer ABILITY_ID = 'A008'
    endglobals

    /*
    --------------------------------------------------------------------------
    # 对话框部分
    --------------------------------------------------------------------------
    * param 
    */
    private struct PkDialog extends array

        private static button btn_accept // 接受
        private static button btn_reject // 拒绝

        private static method anon_a096629ed7da takes nothing returns nothing
            local Dialog dlg = Dialog.get(GetClickedDialog())
            local button btn = GetClickedButton()
            local Pk pk = dlg.data

            if not (pk.m_isCancel) then
                if btn == btn_accept then
                    call pk.begin()
                elseif btn == btn_reject then
                    call pk.cancel()
                endif
            endif

            call dlg.clear()
            set btn = null
        endmethod

        static method showToChallenge takes Pk pk returns nothing
            local Dialog dlg = Dialog.create()
            local unit us = pk.m_pkua.m_unit
            local unit ua = pk.m_pkub.m_unit
            
            set dlg.data = pk

            call dlg.setMessage(GetUnitName(us) + " |cffffffff向你发出了决斗|r")
            
            set btn_accept = dlg.addButton("|cffc8c6bc接受|r", KEY_A)
            set btn_reject = dlg.addButton("|cffffd700拒绝|r", KEY_ESC)

            call dlg.setClickAction(function thistype.anon_a096629ed7da)
            call dlg.display(GetOwningPlayer(ua), true)

            set us = null
            set ua = null
        endmethod

    endstruct

    /*
    --------------------------------------------------------------------------
    # 技能部分
    --------------------------------------------------------------------------
    * param 
    */
    private function SpellEnd takes Spell spell returns nothing
        call spell.destroy()
    endfunction

    private function SpellAction takes Spell spell returns nothing
    endfunction

    private function anon_6828862fd35c takes nothing returns nothing  // 初始化PK数据
        local Pk pk = ReleaseTimer(GetExpiredTimer())
        local Spell spell = Spell.get(pk.m_pkua.m_unit, ABILITY_ID)
        call pk.cancel()
        call SpellEnd(spell)
    endfunction

    private function SpellEffect takes nothing returns nothing
        local Spell spell
        local unit us
        local unit ua
        local Pk pk

        if not (IsWidgetAlive(SpellEvent.CastingUnit)) then
            return
        endif

        set spell = Spell.create(SpellEvent.CastingUnit, ABILITY_ID)
        
        set us = SpellEvent.CastingUnit
        set ua = SpellEvent.TargetUnit

        set pk = Pk.create(us, ua)

        call TimerStart(NewTimerEx(pk), 10.00, false, function anon_6828862fd35c)
        
        call DisplayTimedTextToPlayer(GetOwningPlayer(us), 0, 0, 1.0, "|cffffd700等待目标决定|r")

        // 弹出选择框
        call PkDialog.showToChallenge.execute(pk)

        set us = null
        set ua = null
    endfunction

    private function SpellChannel takes nothing returns nothing
        local unit us = SpellEvent.CastingUnit
        local unit ua = SpellEvent.TargetUnit
        local PkUnit pkSource
        local PkUnit pkTarget

        if IsUnitType(ua, UNIT_TYPE_HERO) /*
            */and IsPlayerPlaying(GetOwningPlayer(ua)) /*
            */and IsPlayerControllerIsUser(GetOwningPlayer(ua)) /*
            */and IsUnitAlly(us, GetOwningPlayer(ua)) /*
            */then
            
            set pkSource = PkUnit.get(us)
            set pkTarget = PkUnit.get(ua)

            if pkTarget.m_isPking then
                call SimError(GetOwningPlayer(us), "目标错误")
                call IssueImmediateOrderById(us, ORDER_NOTARG_STOP)
            elseif pkSource.m_isPking then
                call SimError(GetOwningPlayer(us), "不能这样做")
                call IssueImmediateOrderById(us, ORDER_NOTARG_STOP)
            endif
        else
            call SimError(GetOwningPlayer(us), "目标错误")
            call IssueImmediateOrderById(us, ORDER_NOTARG_STOP)
        endif
        set us = null
        set ua = null
    endfunction

    private function Init takes nothing returns nothing
        call RegisterSpellChannelResponse(ABILITY_ID, SpellChannel)
        call RegisterSpellEffectResponse(ABILITY_ID, SpellEffect)
    endfunction

endlibrary
